﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Web.Security;
using Htss.Platform.Web;

namespace DreamBox.Web.Box.user
{
    public class avatarBox:ControllerBox
    {
        public ActionResult Load()
        {
            if (Request.RequestType == "POST")
            {
                HttpPostedFileBase imgFile = Request.Files["imgFile"];
                bool result = true;
                if (imgFile == null)
                {
                    VerificationMessage["imgFile"] = "请选择需要上传的图片文件";
                    result = false;
                }
                if (imgFile.ContentLength / 1024 > 2048)
                {
                    VerificationMessage["imgFile"] = "请选择2M以内的图片";
                    result = false;
                }

                //处理头像图片
                BLL.User.Avatar avatar = new BLL.User.Avatar();
                int UserID = int.Parse(Session["UserID"].ToString());
                string img = avatar.Upload(imgFile, UserID.ToString(), Server.MapPath("~/"));

                if (img == "")
                {
                    VerificationMessage["imgFile"] = "请选择有效的图片文件";
                    result = false;
                }
                if (result)
                {
                    //插入数据记录
                    DAL.User.UserInfo user = new DAL.User.UserInfo();
                    user.UpdateAvatar(UserID, img);
                }
            }
            //加载原有头像信息
            DreamBox.DAL.PSelect ps = new DAL.PSelect();
            ViewData["Avatar"] = ps.Select("dbox_User", "Avatar", "UserID=" + Session["UserID"].ToString()).Rows[0]["Avatar"].ToString();
            
            return View("user_avatar");
        }
    }
}
